package cn.kgc.controller;


import cn.kgc.pojo.SmbmsAddress;
import cn.kgc.service.AddressService;
import cn.kgc.utils.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {
    private static Logger logger = Logger.getLogger(AddressController.class);

    @Resource
    AddressService addressService;

    @RequestMapping("/limitAddress")
    public String limitAddress(@RequestParam( value = "currentPageNo",required = false)Integer currentPageNo,
                               @RequestParam(value = "pageSize",required = false)Integer pageSize,
                               @RequestParam(value = "contact",required = false)String contact,
                               Model model){

        PageInfo pageInfo4 = new PageInfo();

        if (currentPageNo !=null){
            pageInfo4.setCurrentPageNo(currentPageNo);
        }
        if (pageSize !=null){
            pageInfo4.setPageSize(pageSize);
        }

        int count = addressService.getAddressConut(contact);
        pageInfo4.setTotalConut(count);

        List<SmbmsAddress> list = addressService.getAddressBypage(pageInfo4.getCurrentPageNo(),pageInfo4.getPageSize(),contact);
        pageInfo4.setData(list);
        model.addAttribute("pageAddress",pageInfo4);
        model.addAttribute("contact",contact);
        return "indexAddress";
    }


    @RequestMapping("/toAddAddress")
    public String toAddAddress(){
        return "addressAdd";
    }

    @RequestMapping("/addAddress")
    public String addAddress(SmbmsAddress smbmsAddress){
        boolean flag = addressService.addAddress(smbmsAddress);
        if (flag){
            return "redirect:/address/limitAddress";
        }
        return "addressAdd";
    }


    @RequestMapping("/toDel")
    public String toDel(@RequestParam(value = "id",required = false) Integer id){
        boolean flag = addressService.delAddress(id);
        if (flag){
            return "redirect:/address/limitAddress";
        }
        return "indexAddress";
    }

    @RequestMapping("/toView")
    public String toView(@RequestParam(value = "id",required = false) Integer id,
                         Model model){
        SmbmsAddress smbmsAddress = addressService.getAddressId(id);
        model.addAttribute("adds",smbmsAddress);
        return "addressView";
    }

    @RequestMapping("/toUpdate")
    public String toAddressUpdate(@RequestParam("id")Integer id,Model model){
        SmbmsAddress smbmsAddress = addressService.getAddressId(id);
        model.addAttribute("address",smbmsAddress);
        return "addressUpdate";
    }
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    public String update(SmbmsAddress smbmsAddress){
        boolean flag = addressService.updateAddress(smbmsAddress);
        if (flag){
            return "redirect:/address/limitAddress";
        }else {
            return "addressUpdate";
        }
    }
}
